﻿using Furion.DataEncryption;
using Furion.DependencyInjection;
using GWB.Application.System.UserManage.Dtos;
using GWB.Core;
using GWB.Core.TableEntity;
using GWB.Supplier;
using Mapster;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GWB.Application.System.UserManage.UserService
{
    /// <summary>
    /// 用户管理
    /// </summary>
    public class UserService : SqlSugarDb, ITransient, IUserService
    {

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<Result<UserOutDto>> Add(UserCreateOrUpdateInputDto inputDto)
        {
            var result = new Result<UserOutDto>() { IsSuccess = true };
            //查询是否有重复账号
            var user =await SqlSugarDb.Db.Queryable<User>().FirstAsync(x=>x.Account==inputDto.Account);
            if (user!=null)
            {
                result.Msg = "用户账号账号已存在";
                result.IsSuccess = false;
                return result;
            }
            inputDto.Password  = MD5Encryption.Encrypt("123456");
            var data =await UserDal.Create(inputDto.Adapt<User>());
            result.Data = data.Adapt<UserOutDto>();
            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<bool> Delete(List<long> ids)
        {
            return await UserDal.Delete(ids);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<PageResult<UserOutDto>> Page(UserPageInputDto inputDto)
        {
            RefAsync<int> total = 0;
            var data = await SqlSugarDb.Db.Queryable<Core.TableEntity.User>()
                .OrderBy(x => x.CreateAt, OrderByType.Desc)
                .Select(x => new UserOutDto() { Id = x.Id.SelectAll() })
                .ToPageListAsync(inputDto.Current, inputDto.PageSize, total);
            return new PageResult<UserOutDto>()
            {
                Data = data,
                Total = total,
            };
        }



        /// <summary>
        /// 用户编辑数据权限
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<bool> EditUserDataAuth(List<UserDataAuthDto> inputDto)
        {

            using (var tran = SqlSugarDb.Db.UseTran())
            {
               await UserDataAuthDal.Create(inputDto.Adapt<UserDataAuth>());
                tran.CommitTran();//这个提交不能漏掉
            }
            return true;
        }


        /// <summary>
        /// 获取用户数据权限
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<List<UserDataAuthDto>> GetUserDataAuth(long userId)
        {
            var data = await UserDataAuthDal.GetListAsync(x => x.UserId == userId);
           return data.Adapt<List<UserDataAuthDto>>();

        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public  async  Task<bool> Update(UserCreateOrUpdateInputDto inputDto)
        {
            return await SqlSugarDb.Db.Updateable(inputDto.Adapt<Core.TableEntity.User>())
                .IgnoreColumns(it => new { it.CreateAt, it.CreateBy, it.IsDeleted,it.Password })
                .ExecuteCommandHasChangeAsync();

        }
    }
}
